package com.jyw.activity;

import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jyw.R;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;

import java.io.IOException;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/12
 */

public class ChouJiangActivity extends BaseActivity implements View.OnClickListener {
    private View vClose;
    private ImageView ivPan, ivBar;
    private TextView tvCi;
    private int num;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choujiang);

        vClose = findViewById(R.id.v_close);
        ivPan = (ImageView) findViewById(R.id.iv_pan);
        ivBar = (ImageView) findViewById(R.id.iv_bar);
        tvCi = (TextView) findViewById(R.id.tv_ci);

        vClose.setOnClickListener(this);
        ivBar.setOnClickListener(this);

        loadData();
    }

    private void loadData() {
        Request request = new Request.Builder().url(Server.GET_LUCK_DRAW
                + "?phone_data=" + getPrefs().getString("phone", "")
        ).build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(ChouJiangActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                num = Integer.valueOf(response.body().string());
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... params) {
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                        SpannableString ss = new SpannableString("您还有" + num + "次抽奖机会");
                        ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ss.setSpan(new ForegroundColorSpan(Color.WHITE), 3, ss.length() - 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ss.setSpan(new ForegroundColorSpan(Color.RED), ss.length() - 5, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tvCi.setText(ss);
                    }
                }.execute();
            }
        });
    }

    private void luckDraw() {
        Request request = new Request.Builder().url(Server.LUCK_DRAW
                + "?phone_data=" + getPrefs().getString("phone", "")
        ).build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(ChouJiangActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.body().string().equals("1")) {
                    new AsyncTask<Void, Void, Void>() {
                        @Override
                        protected Void doInBackground(Void... params) {
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void aVoid) {
                            super.onPostExecute(aVoid);
                            num--;
                            SpannableString ss = new SpannableString("您还有" + num + "次抽奖机会");
                            ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            ss.setSpan(new ForegroundColorSpan(Color.WHITE), 3, ss.length() - 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            ss.setSpan(new ForegroundColorSpan(Color.RED), ss.length() - 5, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tvCi.setText(ss);
                            Toast.makeText(ChouJiangActivity.this, "恭喜您抽到了5元红包。请关注您的余额。", Toast.LENGTH_LONG).show();
                        }
                    }.execute();
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.v_close:
                finish();
                break;
            case R.id.iv_bar:
                float res = new Random().nextInt(4);
                ObjectAnimator animator = ObjectAnimator.ofFloat(ivPan, "rotation", 0f, 720f + 1 * 72f);
                animator.setInterpolator(new AccelerateDecelerateInterpolator());
                animator.setDuration(3000);
                animator.start();

                luckDraw();

                switch ((int) res) {
                    case 4:
                        /*Toast.makeText(ChouJiangActivity.this,"")*/
                        break;
                }
        }
    }
}
